package org.example.myleet.rosalind.utils;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

public class Quartet {
    Set<String> ab;
    Set<String> cd;

    public Quartet(String a, String b, String c, String d) {
        ab = new HashSet<>(2);
        ab.add(a);
        ab.add(b);
        cd = new HashSet<>(2);
        cd.add(c);
        cd.add(d);
    }

    //关键是做好去重的逻辑，ab和cd分别已经去重，之后还要做交换去重
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Quartet quartet = (Quartet) o;
        return (ab.containsAll(quartet.ab) && cd.containsAll(quartet.cd)) || (ab.containsAll(quartet.cd) && cd.containsAll(quartet.ab));
    }

    //关键是做好去重的逻辑，两个都计算hashCode再合到一起，即使交换了也得到一样的值
    @Override
    public int hashCode() {
        return Objects.hash(ab) + Objects.hashCode(cd);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("{");
        Iterator<String> it = ab.iterator();
        while (it.hasNext()) {
            sb.append(it.next()).append(", ");
        }
        sb.delete(sb.length() - 2, sb.length());
        sb.append("} {");
        it = cd.iterator();
        while (it.hasNext()) {
            sb.append(it.next()).append(", ");
        }
        sb.delete(sb.length() - 2, sb.length());
        sb.append('}');
        return sb.toString();
    }
}
